package upm.pb4agent.team3;

import java.util.ArrayList;
import java.util.Arrays;

public class Estrategia {
	
	ArrayList<EstrategiaBean> estrategiaDockVector = new ArrayList<EstrategiaBean>(4);
	
	EstrategiaBean estrategiaBeanDockA;
	EstrategiaBean estrategiaBeanDockB;
	EstrategiaBean estrategiaBeanDockC;
	EstrategiaBean estrategiaBeanDockD;
		
	private double rower1Strength;	
	private double rower1Resistance;
	private double rower1Agility;	
		
	private double rower2Strength;	
	private double rower2Resistance;
	private double rower2Agility;
	
	private double rower3Strength;	
	private double rower3Resistance;
	private double rower3Agility;
	
	private double rower4Strength;
	private double rower4Resistance;	
	private double rower4Agility;
	
	int strengthOrderKey[] = new int[Constant.MAX_NUMBER_OF_ROWERS];
	double strengthOrderValues[] = new double[Constant.MAX_NUMBER_OF_ROWERS];
	
	int agilityOrderKey[] = new int[Constant.MAX_NUMBER_OF_ROWERS];
	double agilityOrderValues[] = new double[Constant.MAX_NUMBER_OF_ROWERS];
	
	int resistanceOrderKey[] = new int[Constant.MAX_NUMBER_OF_ROWERS];
	double resistanceOrderValues[] = new double[Constant.MAX_NUMBER_OF_ROWERS];
	
	public Estrategia(){
		readPhysicalProperties();
		burbujaKeyValuesStrength();
		burbujaKeyValuesAgility();
		burbujaKeyValuesResistance();
		//
		
		estrategiaBeanDockA = new EstrategiaBean();
		estrategiaBeanDockA.setNameRower(getNameRower(resistanceOrderKey[0]));//System.out.println("DockA: CON" + getNameRower(resistanceOrderKey[0]));
		estrategiaBeanDockA.setNameDock(Constant.NAME_DOCK_A);
		estrategiaBeanDockA.setDirection(MyEnumDirection.FORWARD.getDirection());
		estrategiaBeanDockA.setStrength(MyEnumStrength.STRONG.getStrength());
						
		estrategiaBeanDockB = new EstrategiaBean();
		estrategiaBeanDockB.setNameRower(getNameRower(resistanceOrderKey[3]));//System.out.println("DockB: CON" + getNameRower(resistanceOrderKey[1]));
		estrategiaBeanDockB.setNameDock(Constant.NAME_DOCK_B);
		estrategiaBeanDockB.setDirection(MyEnumDirection.FORWARD.getDirection());
		estrategiaBeanDockB.setStrength(MyEnumStrength.MEDIUM.getStrength());
		
		estrategiaBeanDockC = new EstrategiaBean();
		estrategiaBeanDockC.setNameRower(getNameRower(resistanceOrderKey[1]));//System.out.println("DockC: CON" + getNameRower(resistanceOrderKey[2]));
		estrategiaBeanDockC.setNameDock(Constant.NAME_DOCK_C);
		estrategiaBeanDockC.setDirection(MyEnumDirection.FORWARD.getDirection());
		estrategiaBeanDockC.setStrength(MyEnumStrength.STRONG.getStrength());
				
		estrategiaBeanDockD = new EstrategiaBean();
		estrategiaBeanDockD.setNameRower(getNameRower(resistanceOrderKey[2]));//System.out.println("DockD: CON" + getNameRower(resistanceOrderKey[3]));
		estrategiaBeanDockD.setNameDock(Constant.NAME_DOCK_D);
		estrategiaBeanDockD.setDirection(MyEnumDirection.FORWARD.getDirection());
		estrategiaBeanDockD.setStrength(MyEnumStrength.WEAK.getStrength());
	}
	
	public void burbujaKeyValuesStrength(){
		double tmp = 0;
		int tmpKey = 0;
		for(int i = 0; i<strengthOrderValues.length-1;i++){
			tmp = (Double)strengthOrderValues[i];
			
			for(int j = i + 1; j<strengthOrderValues.length-1;j++){
				if((Double) strengthOrderValues[i] < (Double) strengthOrderValues[j]){					
					tmp = (Double)strengthOrderValues[i];
					strengthOrderValues[i] = strengthOrderValues[j];
					strengthOrderValues[j] = tmp;
					
					tmpKey = (Integer) strengthOrderKey[i];
					strengthOrderKey[i] = strengthOrderKey[j];
					strengthOrderKey[j] = tmpKey;
				}
			}
		}		
		//System.out.println("Strength key: " + Arrays.toString(strengthOrderKey));
		//System.out.println("Strength Values: " + Arrays.toString(strengthOrderValues));
	}
	
	public void burbujaKeyValuesAgility(){
		double tmp = 0;
		int tmpKey = 0;
		for(int i = 0; i<agilityOrderValues.length-1;i++){
			tmp = (Double)agilityOrderValues[i];
			
			for(int j = i + 1; j<agilityOrderValues.length-1;j++){
				if((Double) agilityOrderValues[i] < (Double) agilityOrderValues[j]){					
					tmp = (Double)agilityOrderValues[i];
					agilityOrderValues[i] = agilityOrderValues[j];
					agilityOrderValues[j] = tmp;
					
					tmpKey = (Integer) agilityOrderKey[i];
					agilityOrderKey[i] = agilityOrderKey[j];
					agilityOrderKey[j] = tmpKey;
				}
			}
		}		
		//System.out.println("Agility key: " + Arrays.toString(agilityOrderKey));
		//System.out.println("Agility Values: " + Arrays.toString(agilityOrderValues));
	}
	
	public void burbujaKeyValuesResistance(){
		double tmp = 0;
		int tmpKey = 0;
		for(int i = 0; i<resistanceOrderValues.length-1;i++){
			tmp = (Double)resistanceOrderValues[i];
			
			for(int j = i + 1; j<resistanceOrderValues.length-1;j++){
				if((Double) resistanceOrderValues[i] < (Double) resistanceOrderValues[j]){					
					tmp = (Double)resistanceOrderValues[i];
					resistanceOrderValues[i] = resistanceOrderValues[j];
					resistanceOrderValues[j] = tmp;
					
					tmpKey = (Integer) resistanceOrderKey[i];
					resistanceOrderKey[i] = resistanceOrderKey[j];
					resistanceOrderKey[j] = tmpKey;
				}
			}
		}		
		//System.out.println("Resistance key: " + Arrays.toString(resistanceOrderKey));
		//System.out.println("Resistance Values: " + Arrays.toString(resistanceOrderValues));
	}
	
	
	
	
	public void setConfigurationDockA(){		
		try{
			estrategiaDockVector.clear();
			estrategiaBeanDockA.setNameSeat(Constant.NAME_SEAT_1);
			estrategiaBeanDockA.setNameOar1(Constant.NAME_OAR_1);
			
			estrategiaDockVector.add(0,estrategiaBeanDockA);
		}catch (Exception e) {
			System.out.println(Constant.NAME_ESTRATEGIA + ".configurationDockA() , error: "+ e.toString());
		}
	}
	
	public void setConfigurationDockB(){
		try{			
			estrategiaDockVector.clear();
			estrategiaBeanDockB.setNameSeat(Constant.NAME_SEAT_3);
			estrategiaBeanDockB.setNameOar1(Constant.NAME_OAR_2);
			
			estrategiaDockVector.add(0,estrategiaBeanDockA);
			estrategiaDockVector.add(1,estrategiaBeanDockB);
		}catch (Exception e) {
			System.out.println(Constant.NAME_ESTRATEGIA + ".configurationDockB() , error: "+ e.toString());
		}
	}
	
	public void setConfigurationDockC(){
		try{
			estrategiaDockVector.clear();
			//
			estrategiaBeanDockC.setNameSeat(Constant.NAME_SEAT_4);
			estrategiaBeanDockC.setNameOar1(Constant.NAME_OAR_3);
			
			estrategiaDockVector.add(0,estrategiaBeanDockA);
			estrategiaDockVector.add(1,estrategiaBeanDockB);
			estrategiaDockVector.add(2,estrategiaBeanDockC);
		}catch (Exception e) {
			System.out.println(Constant.NAME_ESTRATEGIA + ".configurationDockC() , error: "+ e.toString());
		}	
	}
	
	public void setConfigurationDockD(){
		try{
			estrategiaDockVector.clear();
			//
			estrategiaBeanDockD.setNameSeat(Constant.NAME_SEAT_5);
			estrategiaBeanDockD.setNameOar1(Constant.NAME_OAR_4);
			
			estrategiaDockVector.add(0,estrategiaBeanDockA);
			estrategiaDockVector.add(1,estrategiaBeanDockB);
			estrategiaDockVector.add(2,estrategiaBeanDockC);
			estrategiaDockVector.add(3,estrategiaBeanDockD);
		}catch (Exception e) {
			System.out.println(Constant.NAME_ESTRATEGIA + ".configurationDockD() , error: "+ e.toString());
		}
	}
	
		
	private void readPhysicalProperties(){
		ReadConfiguration.getInstance(0);	
		//
		String propTmp = ReadConfiguration.getValue("rower1_strength");
		if(propTmp != null){
			this.rower1Strength = Double.parseDouble(propTmp);	
			strengthOrderValues[0] = this.rower1Strength;
			strengthOrderKey[0] = 1;
		}
		propTmp = ReadConfiguration.getValue("rower1_resistance");
		if(propTmp != null){
			this.rower1Resistance = Double.parseDouble(propTmp);
			resistanceOrderValues[0] = this.rower1Resistance;
			resistanceOrderKey[0] = 1;
		}		
		propTmp = ReadConfiguration.getValue("rower1_agility");
		if(propTmp != null){
			this.rower1Agility = Double.parseDouble(propTmp);
			agilityOrderValues[0] = this.rower1Agility;
			agilityOrderKey[0] = 1;
		}
		
		
		propTmp = ReadConfiguration.getValue("rower2_strength");
		if(propTmp != null){
			this.rower2Strength = Double.parseDouble(propTmp);	
			strengthOrderValues[1] = this.rower2Strength;
			strengthOrderKey[1] = 2;
		}
		propTmp = ReadConfiguration.getValue("rower2_resistance");
		if(propTmp != null){
			this.rower2Resistance = Double.parseDouble(propTmp);
			resistanceOrderValues[1] = this.rower2Resistance;
			resistanceOrderKey[1] = 2;
		}		
		propTmp = ReadConfiguration.getValue("rower2_agility");
		if(propTmp != null){
			this.rower2Agility = Double.parseDouble(propTmp);
			agilityOrderValues[1] = this.rower2Agility;
			agilityOrderKey[1] = 2;
		}
		
		propTmp = ReadConfiguration.getValue("rower3_strength");
		if(propTmp != null){
			this.rower3Strength = Double.parseDouble(propTmp);
			strengthOrderValues[2] = this.rower3Strength;
			strengthOrderKey[2] = 3;
		}
		propTmp = ReadConfiguration.getValue("rower3_resistance");
		if(propTmp != null){
			this.rower3Resistance = Double.parseDouble(propTmp);
			resistanceOrderValues[2] = this.rower3Resistance;
			resistanceOrderKey[2] = 3;
		}		
		propTmp = ReadConfiguration.getValue("rower3_agility");
		if(propTmp != null){
			this.rower3Agility = Double.parseDouble(propTmp);
			agilityOrderValues[2] = this.rower3Agility;
			agilityOrderKey[2] = 3;
		}
		
		
		propTmp = ReadConfiguration.getValue("rower4_strength");
		if(propTmp != null){
			this.rower4Strength = Double.parseDouble(propTmp);
			strengthOrderValues[3] = this.rower4Strength;
			strengthOrderKey[3] = 4;
		}
		propTmp = ReadConfiguration.getValue("rower4_resistance");
		if(propTmp != null){
			this.rower4Resistance = Double.parseDouble(propTmp);
			resistanceOrderValues[3] = this.rower4Resistance;
			resistanceOrderKey[3] = 4;
		}		
		propTmp = ReadConfiguration.getValue("rower4_agility");
		if(propTmp != null){
			this.rower4Agility = Double.parseDouble(propTmp);
			agilityOrderValues[3] = this.rower4Agility;
			agilityOrderKey[3] = 4;
		}
	}
	
	public String getNameRower(int num){
		 switch(num) {
		 case 1: 
		     return Constant.NAME_ROWER_1;
		 case 2: 
			 return Constant.NAME_ROWER_2;
		 case 3: 
			 return Constant.NAME_ROWER_3;
		 case 4: 
			 return Constant.NAME_ROWER_4; 
		 default: 
		     return "";
		 }
	}

}
